﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;
using DevExpress.XtraEditors.Controls;

namespace DevDemo
{
    public partial class ListBoxDemo : Form
    {
        DataTable tblDatas = new DataTable("Datas");

        public ListBoxDemo()
        {
            InitializeComponent();

            returnDt();

            BindData();

            List<string> s = new string[] { "1", "2" }.ToList();

            // index listcontrol checked
            //this.listBoxControl1.SetSelected(1, true);
        }

        public void BindData()
        {
            #region DATATABLE BIND LISTBOX

            this.listBoxControl1.DisplayMember = "Product";

            this.listBoxControl1.ValueMember = "ID";

            this.listBoxControl1.DataSource = tblDatas;

            #endregion
        }

        #region Datatable

        public DataTable returnDt()
        {
            DataColumn dc = null;

            dc = tblDatas.Columns.Add("ID", Type.GetType("System.Int32"));

            dc.AutoIncrement = true;//自动增加

            dc.AutoIncrementSeed = 1;//起始为1

            dc.AutoIncrementStep = 1;//步长为1

            dc.AllowDBNull = false;//

            dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));

            dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));

            DataRow newRow;

            newRow = tblDatas.NewRow();
            newRow["Product"] = "大话西游";
            newRow["Version"] = "2.0";
            tblDatas.Rows.Add(newRow);

            newRow = tblDatas.NewRow();
            newRow["Product"] = "梦幻西游";
            newRow["Version"] = "3.0";
            tblDatas.Rows.Add(newRow);

            return tblDatas;
        }
        #endregion

        /// <summary>
        /// add
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            DataRow newRow;

            newRow = tblDatas.NewRow();
            newRow["Product"] = "天龙八部";
            newRow["Version"] = "3.0";
            tblDatas.Rows.Add(newRow);

            BindData();
        }

        /// <summary>
        /// del
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            BaseListBoxControl.SelectedItemCollection lists = listBoxControl1.SelectedItems;

            string ID = ((DataRowView)lists[0]).Row["ID"].ToString();

            DataRow[] drs = tblDatas.Select("ID = '" + ID + "'");

            if (drs.Length > 0)
            {
                tblDatas.Rows.RemoveAt(tblDatas.Rows.IndexOf(drs[0]));

                BindData();
            }
        }

        /// <summary>
        /// update
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            BaseListBoxControl.SelectedItemCollection lists = listBoxControl1.SelectedItems;

            string ID = ((DataRowView)lists[0]).Row["ID"].ToString();

            DataRow[] drs = tblDatas.Select("ID = '" + ID + "'");

            if (drs.Length > 0)
            {
                int index = tblDatas.Rows.IndexOf(drs[0]);

                tblDatas.Rows[index]["Product"] = "天龙八部 V3.0";

                BindData();
            }
        }

        /// <summary>
        /// select
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            ArrayList ar = new ArrayList();

            foreach (DataRow item in tblDatas.Rows)
            {
                ar.Add(Convert.ToString(item["Product"]));
            }

            Text = string.Join(",", (string[])ar.ToArray(typeof(string)));
        }

        /// <summary>
        /// meunitem click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.Text.Trim().Length > 0)
            {
                DataRow[] drs = tblDatas.Select("ID = '" + this.Text.Trim() + "'");

                if (drs.Length > 0)
                {
                    tblDatas.Rows.RemoveAt(tblDatas.Rows.IndexOf(drs[0]));

                    BindData();
                }
            }
        }

        /// <summary>
        /// double checked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxControl1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            BaseListBoxControl.SelectedItemCollection lists = listBoxControl1.SelectedItems;

            MessageBox.Show(((DataRowView)lists[0]).Row["ID"].ToString(), "double checked");
        }

        /// <summary>
        /// mouse left right get value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
        {
            // this.listBoxControl1.SelectedIndex.ToString(); 

            BaseListBoxControl.SelectedItemCollection lists = listBoxControl1.SelectedItems;

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                Point p = e.Location;

                int index = this.listBoxControl1.IndexFromPoint(p);

                this.listBoxControl1.SetSelected(index, true);

                Text = ((DataRowView)lists[0]).Row["ID"].ToString();
            }

            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Text = ((DataRowView)lists[0]).Row["ID"].ToString();
            }
        }
    }
}
